package com.ruoyi.web.controller.pssd;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.pssd.domain.PsdDrug;
import com.ruoyi.pssd.mapper.PsdDrugMapper;
import com.ruoyi.pssd.service.PsdDrugService;
import com.ruoyi.system.domain.SysRegister;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/pssd/drug")
public class PsdDrugController extends BaseController {
    @Autowired
    private PsdDrugService psdDrugService;
    /**
     * 获取列表
     */
    @PreAuthorize("@ss.hasPermi('pssd:drug:list')")
    @GetMapping("/list")
    public TableDataInfo list(PsdDrug drug) {
        startPage();
        List<PsdDrug> list = psdDrugService.selectDrugList(drug);
        return getDataTable(list);
    }

    /**
     * 新增挂号信息
     */
    @PreAuthorize("@ss.hasPermi('psd:drug:add')")
    @Log(title = "药品信息", businessType = BusinessType.INSERT)
    @PutMapping("/add")
    public AjaxResult add(@Validated @RequestBody PsdDrug drug) {
        drug.setUpdateBy(getUsername());
        return toAjax(psdDrugService.insertDrug(drug));
    }

    /**
     * 根据id查询挂号信息
     */
    @PreAuthorize("@ss.hasPermi('pssd:drug:query')")
    @GetMapping(value = "/{drugId}")
    public AjaxResult getInfo(@PathVariable Long drugId) {
        return success(psdDrugService.selectDrugById(drugId));
    }

    @PreAuthorize("@ss.hasPermi('pssd:drug:edit')")
    @Log(title = "药品信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody PsdDrug drug) {
        drug.setUpdateBy(getUsername());
        return toAjax(psdDrugService.updateDrugById(drug));
    }

    @PreAuthorize("@ss.hasPermi('pssd:drug:remove')")
    @Log(title = "删除药品信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{drugIds}")
    public AjaxResult remove(@PathVariable Long[] drugIds) {
        return toAjax(psdDrugService.deleteDrugByIds(drugIds));
    }

    @Autowired
    private PsdDrugMapper psdDrugMapper;

    @PreAuthorize("@ss.hasPermi('pssd:drug:vender')")
    @GetMapping("/vender")
    public TableDataInfo get(){
        startPage();
        List<PsdDrug> list = psdDrugMapper.selectVender();
        return getDataTable(list);
    }
}
